package com.blockmeta.bbs.businesslibrary.umeng.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.umeng.AnimatorUtil;
import com.blockmeta.bbs.businesslibrary.umeng.BaseShareDialog;
import com.blockmeta.bbs.businesslibrary.umeng.MEDIA_TYPE;
import com.blockmeta.bbs.businesslibrary.umeng.ScreenShotUtil;
import com.blockmeta.bbs.businesslibrary.umeng.ShareResTask;
import com.blockmeta.bbs.businesslibrary.umeng.ShareRouter;
import com.blockmeta.bbs.businesslibrary.umeng.SharedUtils;
import com.blockmeta.bbs.businesslibrary.util.CodeUtils;

/**
 * @author AirThor
 * @date 2020/3/25.
 */
public class F10Share extends BaseShareDialog {
    private View mShareView;

    public F10Share(Context context, ShareRouter shareRouter) {
        super(context, shareRouter);
    }

    @Override
    protected void initContent() {
        ImageView wechat = find(R.id.iv_wechat);
        ImageView ciecle =  find(R.id.iv_wechat_circle);
        ImageView pic =  find(R.id.iv_long_pic);
        ImageView qq =  find(R.id.iv_qq);
        ImageView weibo =  find(R.id.iv_weibo);
        TextView dismiss =  find(R.id.tv_dismiss);
        mShareView = find(R.id.scroll_share_image);
        TextView tvCoinName = find(R.id.share_tv_coin_name);
        TextView tvCoinInfo = find(R.id.share_tv_coin_introduce);
        ImageView qr = find(R.id.share_iv_qr);
        ImageView ivTop=find(R.id.share_iv_top_f10);
        ImageView ivBottom=find(R.id.share_iv_bottom_f10);

        Bitmap[] bitmaps = mShareRouter.getDisplayBitmaps();

        ivTop.setImageBitmap(bitmaps[0]);
        if (bitmaps[1] != null) {
            ivBottom.setImageBitmap(bitmaps[1]);
        }

        int wh = DimentionsUtil.dp2px(70);
        Bitmap bitmap = CodeUtils.createImage(sUrl, wh, wh, BitmapFactory.decodeResource(BaseApp.getApp().getResources(),R.drawable.qr_logo));
        qr.setImageBitmap(bitmap);

        tvCoinName.setText(mShareRouter.getTitle());
        tvCoinInfo.setText(mShareRouter.getDesc());

        View shareBoard = find(R.id.v_share_bg);
        AnimatorUtil.bottomToTopAnim(shareBoard,500,false,0f);
        AnimatorUtil.bottomToTopAnim(wechat,1100,1f);
        AnimatorUtil.bottomToTopAnim(ciecle,1000,1f);
        AnimatorUtil.bottomToTopAnim(weibo,900,1f);
        AnimatorUtil.bottomToTopAnim(qq,800,1f);
        AnimatorUtil.bottomToTopAnim(pic,700,1f);

        wechat.setOnClickListener(this);
        ciecle.setOnClickListener(this);
        pic.setOnClickListener(this);
        qq.setOnClickListener(this);
        weibo.setOnClickListener(this);
        dismiss.setOnClickListener(this);
    }

    @Override
    protected int setContentViews() {
        return R.layout.view_share_f10;
    }

    @Override
    protected void setAttributes() {
        setAttribute(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    }

    @Override
    protected void startShare(final MEDIA_TYPE mediaType) {
        ShareResTask.doTask(new ShareResTask.NormalBitmapTask(mShareView) {
            @Override
            public void doOnUIThread(Bitmap bitmap) {
                share(bitmap,mediaType);
                safeDismiss();
            }
        });
    }

    private void share(Bitmap bitmap, MEDIA_TYPE mediaType) {
        ScreenShotUtil.zoomImg(bitmap, 1080);
        if (mediaType == MEDIA_TYPE.SAVE){
            ShareResTask.doTask(new ShareResTask.SaveImgTask(mShareRouter.getContext(),bitmap));
        }else {
            SharedUtils.sharePic((Activity) mShareRouter.getContext(),mediaType.mMedia,bitmap,mShareRouter.getUMShareListener());
        }
    }
}
